<?php
namespace app\api\service;

use app\BaseController;
use think\facade\Cache;

class LogisticsService extends BaseController
{
    /**
     * Su ang
     * 查询物流信息 查询后存入redis 下次查询直接走redis
     * @return \think\response\Json
     */
    public static function logistics($no,$type)
    {
        $redis =  Cache::store('redis');
        $data  = $redis->has($no);
        if (empty($data)){
            $comment  = self::logisticsSee($no,$type);
            $jsonData =  json_encode($comment);
            $redis->set($no,$jsonData);
        }else{
            $comment =json_decode($redis->get('462053651312771'),true) ;
            return succeedHint($comment,'查询成功');
        }
    }


    public static function logisticsSee($no,$type)
    {
//        echo $no;die();
            //请求网址
            $host = "https://kuaidi101.market.alicloudapi.com";
            $path = "/getExpress"; //请求前缀
            $method = "GET"; //请求方式
            $appcode = "d5e253e9804a478ca9304ebcd582f1c2"; //阿里云 appcode
            $headers = array();
            array_push($headers, "Authorization:APPCODE " . $appcode);
            //$no 订单物流号 $type 快递公司 中通（zhongtong）
                $querys = "NO=$no&TYPE=$type";
            $url = $host . $path . "?" . $querys; //拼接地址
            //curl 请求
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($curl, CURLOPT_FAILONERROR, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HEADER, false);
            if (1 == strpos("$".$host, "https://")){
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            }
            //这里改成false 可以转成json格式
            curl_setopt($curl, CURLOPT_POSTFIELDS,true);
            $data = curl_exec($curl) ;
//            return $data;
            return (json_decode($data,true));
    }
}